<?php
/**
 * lsys orm
 * @author     Lonely <shan.liu@msn.com>
 * @copyright  (c) 2017 Lonely <shan.liu@msn.com>
 * @license    http://www.apache.org/licenses/LICENSE-2.0
 */
namespace LSYS\ORM;
use LSYS\ORM;
class Active extends ORM {
	/**
	 * from table crate orm
	 */
	public function __construct($table_name,$id=NULL) {
		$this->_table_name=$table_name;
		$this->_object_name=$table_name;
		if(empty($this->_entity_name)) $this->_entity_name='\LSYS\ORM\EntityActive';
		$this->_init();
		$_columns = isset(ORM::$_init_cache[$this->_object_name]['_table_columns'])?ORM::$_init_cache[$this->_object_name]['_table_columns']:false;
		$_primary_key = isset(ORM::$_init_cache[$this->_object_name]['_primary_key'])?ORM::$_init_cache[$this->_object_name]['_primary_key']:false;
		if($_columns===FALSE||$_primary_key===FALSE){
			$arr=array();
			$db=isset($init ['_db'])?$init ['_db']:$this->_db;
			foreach ($db->list_columns($db->quote_table($this->_table_name)) as $k=>$v){
				if($v['key']==='PRI') $_primary_key=ORM::$_init_cache[$this->_object_name]['_primary_key']=$k;
				$arr[$k]=$v['column_default'];
			}
			$_columns=ORM::$_init_cache[$this->_object_name]['_table_columns']=$arr;
		}
		$this->_table_defaults=$_columns;
		$this->_table_columns=array_keys($_columns);
		if ($_primary_key===false) throw new Exception(__("table :table Does not exist  primary key",array("table"=>$this->_table_name)));
		$this->_primary_key=$_primary_key;
		$this->_init_find($id);
	}
}

